package com.example.my_2048.view;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.GridLayout;

import com.example.my_2048.R;
import com.example.my_2048.activity.Main;
import com.example.my_2048.been.Game;
import com.example.my_2048.util.Constants;

/**
 * Created by tang on 16/8/18.
 */
public class GameView extends GridLayout implements View.OnTouchListener{

    public CardView[][] cardViews;
    public Game game;
    public static GameView gameView;
    private float fStartX = 0,fStartY = 0,fDPX,fDPY;

    private int iTouchDirection;


    public static GameView getGameView() {
        return gameView;
    }

    public GameView(Context context) {
        super(context);
        init();
    }

    public GameView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public GameView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }



    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {

        int cardWidth = (Math.min(w, h) ) / 4;
        createCard(cardWidth, h/4);

        refreshPanel();
    }


    // TODO: 16/8/19 最操蛋的地方,设置GridLayout的间距,这些都是套出来的,还要分析它的算法。
    private void createCard(int cardWidth, int cardHeight) {
        int h1=20,h2=35;


        for (int i = 0; i < 4; i++) {
            h1=20;
            //h2=20;
            for (int j = 0; j < 4; j++) {
                h1+=20;
//                h2+=20;
                cardViews[i][j]=new CardView(getContext());
                cardViews[i][j].setX(h1);
                cardViews[i][j].setY(h2);
                addView(cardViews[i][j],cardWidth-35,cardHeight-35);
            }
            //h1+=10;
            h2+=20;
        }
    }

    private void init() {
        gameView=this;
        setBackgroundResource(R.color.colorGamePanel);
        setColumnCount(4);
        setAlignmentMode(ALIGN_BOUNDS);
        this.setOnTouchListener(this);
        cardViews=new CardView[4][4];
        game=new Game();
        game.Random();

    }

    public   void refreshPanel() {
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                cardViews[i][j].setiScores(game.getArrsGameElement()[i][j]);
                cardViews[i][j].showTvScores();
                //Log.i(Constants.TAG, "refreshPanel: 显示Card的值");
            }
        }

    }


    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {



        switch (motionEvent.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                fStartX=motionEvent.getX();
                fStartY=motionEvent.getY();
                return true;


            case MotionEvent.ACTION_UP:
                /*
                *  这里我发现了一个很好的方法,去访问之前访问我的Activity
                *  是通过一个getMain()方法去获取Main中已经instantiate
                *  的本体,通过该本体的addScores方法从而改变TextView的值。
                * */
//                Main.getMain().addScores(5);
                iTouchDirection=TouchDirection(motionEvent);
                if(game.touchDirection(iTouchDirection))
                {
                    game.Random();

                }
//                Constants.addScore(game.getiSumScore());
                Main.getMain().showCurrentScores();
                refreshPanel();
                if(game.isGameEnd())gameEnd();
                return false;
        }
        return false;
    }

    private void gameEnd() {
        new AlertDialog.Builder(getContext()).setTitle("提醒")
                .setPositiveButton("重新开始", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Main.getMain().resetGame();
                    }
                }).setCancelable(false).show();
    }

    private int TouchDirection(MotionEvent motionEvent) {
        fDPX=Math.abs(motionEvent.getX()-fStartX);
        fDPY=Math.abs(motionEvent.getY()-fStartY);
        // TODO: 16/8/19 这个地方有一个问题  刚刚通过测试发现它记录的是起点与终点 那么如果发现了 画圆圈就会不准确了 当然可能也不用解决
        if(motionEvent.getX()-fStartX>20)
        {
            if(motionEvent.getY()>fStartY){
                if(fDPX>fDPY){
                    return Constants.RIGHT;
                }
                else{
                    return Constants.DOWN;
                }
            }else{
                if(fDPX>fDPY){
                    return Constants.RIGHT;
                }
                else{
                    return Constants.UP;
                }
            }
        }else{
            if(motionEvent.getY()>fStartY){
                if(fDPX>fDPY){
                    return Constants.LEFT;
                }
                else{
                    return Constants.DOWN;
                }
            }else{
                if(fDPX>fDPY){
                    return Constants.LEFT;
                }
                else{
                    return Constants.UP;
                }
            }
        }
    }

}
